package com.web.exiaodao.business.controller;

import com.web.exiaodao.business.service.ReturnshoprecordService;
import com.web.exiaodao.remote.vo.ReturnshoprecordVo;
import com.web.webstart.base.constant.XaConstant;
import com.web.webstart.base.controller.BaseController;
import com.web.webstart.base.exception.BusinessException;
import com.web.webstart.base.util.jpa.WebUitl;
import com.web.webstart.base.util.result.XaResult;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * @author eason.zt
 * @version V1.0
 * @Title: ReturnshoprecordController.java
 * @Package com.web.webstart.business.controller
 * @Description: 退货记录定义表控制器
 * @date 2017-01-22 13:32:17
 */
@Controller
@RequestMapping("/cms/returnshoprecord")
public class ReturnshoprecordController extends BaseController {

    private static final Logger LOGGER = Logger.getLogger(ReturnshoprecordController.class);

    @Autowired
    private ReturnshoprecordService returnshoprecordService;

    /**
     * @param pageSize
     * @param status     //默认查询非删除3的数据,具体类型参照XaConstant.Status
     *                   //@param sortData
     * @param jsonFilter
     * @return
     * @Title: findReturnshoprecordNEStatusPage
     * @Description: 分页查询Returnshoprecord     * @param nextPage
     */
    @ResponseBody
    @RequestMapping(value = "findReturnshoprecordNEStatusPage", method = RequestMethod.POST)
    public XaResult<Page<ReturnshoprecordVo>> findReturnshoprecordNEStatusPage(
            @RequestParam(defaultValue = "0") Integer nextPage,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestParam(defaultValue = "3") Integer status,
            @RequestParam(defaultValue = "[{property:'createTime',direction:'DESC'}]") String sortData,
            @RequestParam(defaultValue = "{}") String jsonFilter
    ) throws BusinessException {
        XaResult<Page<ReturnshoprecordVo>> xr = new XaResult<Page<ReturnshoprecordVo>>();
        Pageable pageable = WebUitl.buildPageRequest(nextPage, pageSize, sortData);
        Map<String, Object> filterParams = WebUitl.getParametersStartingWith(jsonFilter, "search_");
        xr = returnshoprecordService.findListNEStatusByFilter(status, filterParams, pageable);
        return xr;
    }

    /**
     * 退款/退货审核
     *
     * @author zhanglin
     * @date 2017/1/22 下午5:40
     */
    @ResponseBody
    @RequestMapping(value = "refuseAudit", method = RequestMethod.POST)
    public XaResult<String> audit(
            Long id,
            String refundAdvice, //退款审核意见
            Integer refundStatus, //退款审核状态
            HttpServletRequest request, HttpServletResponse response
    ) throws Exception {
        return returnshoprecordService.auditing(id, refundAdvice, refundStatus, request);
    }

    /**
     * @param modelId
     * @return
     * @Title: findReturnshoprecordById
     * @Description: 根据ID查找单条实体
     */
    @ResponseBody
    @RequestMapping(value = "findReturnshoprecordById", method = RequestMethod.POST)
    public XaResult<ReturnshoprecordVo> findReturnshoprecordById(
            @RequestParam Long modelId
    ) throws BusinessException {
        XaResult<ReturnshoprecordVo> xr = new XaResult<ReturnshoprecordVo>();
        try {
            xr = returnshoprecordService.findOne(modelId);
        } catch (Exception e) {
            xr.error(e instanceof BusinessException ? e.getMessage() : XaConstant.Message.error);
            LOGGER.error(XaConstant.Message.error + e.getMessage(), new Throwable(e));
        }
        return xr;
    }
}

